package com.youlian.cloud.spring.config;

import com.youlian.cloud.core.util.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import javax.sql.DataSource;

/**
 * 事务的定义
 *
 * @author Liukx
 * @create 2018-04-16 18:06
 * @email liukx@elab-plus.com
 **/
@EnableTransactionManagement
@Configuration
public class TransactionConfigBean implements TransactionManagementConfigurer {

    @Autowired
    private DataSource dataSource;

    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return getDataSourceTransactionManager();
    }

    @Bean
    public SpringUtils getSpringUtils() {
        SpringUtils springUtils = new SpringUtils();
        return springUtils;
    }

    @Bean
    public DataSourceTransactionManager getDataSourceTransactionManager() {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource);
        return transactionManager;
    }

}
